package com.zk.transaction.dao;

import com.zk.transaction.utils.JDBCUtils2;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;

public class AccountDao {
    /**
     * DAO的扣钱的方法
     * @param from
     * @param money
     * @throws SQLException
     */
    public void outMoney(Connection conn, String from, Double money) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "update account set money = money - ? where name = ?";
        queryRunner.update(conn, sql, money ,from);
    }
    public void outMoney2( String from, Double money) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        Connection conn = JDBCUtils2.getConnection();
        String sql = "update account set money = money - ? where name = ?";
        queryRunner.update(conn, sql, money ,from);
    }
/*	public void outMoney(String from, Double money) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try{
			// 获得连接：
			conn = JDBCUtils2.getConnection();
			// 编写SQL：
			String sql = "update account set money = money - ? where name = ?";
			// 预编译SQL：
			stmt = conn.prepareStatement(sql);
			stmt.setDouble(1, money);
			stmt.setString(2, from);
			stmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
				// conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}*/

    /**
     * DAO中给某个用户加钱的方法:
     * @param to
     * @param money
     * @throws SQLException
     */
    public  void  inMoney(Connection conn,String to, Double money) throws SQLException{
        QueryRunner queryRunner = new QueryRunner();
        String sql = "update account set money = money + ? where name = ?";
        queryRunner.update(conn, sql, money,to);
    }
    public  void  inMoney2(String to, Double money) throws SQLException{
        QueryRunner queryRunner = new QueryRunner();
        Connection conn = JDBCUtils2.getConnection();
        String sql = "update account set money = money + ? where name = ?";
        queryRunner.update(conn, sql, money,to);
    }
	/*public void inMoney(String to, Double money) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try{
			// 获得连接：
			conn = JDBCUtils2.getConnection();
			// 编写SQL：
			String sql = "update account set money = money + ? where name = ?";
			// 预编译SQL：
			stmt = conn.prepareStatement(sql);
			stmt.setDouble(1, money);
			stmt.setString(2, to);
			stmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
				// conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}*/

}
